package com.happyfamily.springboot.crowdsourcingplatform.controller.wang;


import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.ResponseCode;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.UserDto;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Arbitration;
import com.happyfamily.springboot.crowdsourcingplatform.model.Orders;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.UserService;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.ArbitrationService;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.CreditService;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.ExposureService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 仲裁控制类
 */
@Controller
@Slf4j
public class arbitrationController
{
	@Autowired
	private ArbitrationService arbitrationServiceImpl;
	@Autowired
	private CreditService creditServiceImpl;
	@Autowired
	private ExposureService exposureServiceImpl;
	@Autowired
	private UserService userServiceImpl;

	/**
	 * 获取用户的id
	 *
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "获取用户的id", notes = "获取用户的id")
	@OperationLogDetail(detail = "获取用户的id", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	@GetMapping("/api-front/arbitration/getRid")
	@ResponseBody
	public SysUser getRid(HttpServletRequest req)
	{
		SecurityContextImpl securityContext = (SecurityContextImpl) req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String uaccount = securityContext.getAuthentication().getName();
		return creditServiceImpl.searchCredit(uaccount);
	}

	/**
	 * 弹窗我的个人资料
	 *
	 * @return
	 */
	@ApiOperation(value = "跳转弹窗我的个人资料", notes = "跳转弹窗我的个人资料")
	@GetMapping("/api-front/arbitration/myDetails")
	public String myDetails()
	{
		return "frontPage/myDetails";
	}

	/**
	 * 修改昵称
	 *
	 * @param uid
	 * @param uname
	 * @return
	 */
	@ApiOperation(value = "修改昵称", notes = "修改昵称")
	@OperationLogDetail(detail = "修改昵称", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	@PostMapping("/api-front/arbitration/changeName")
	@ResponseBody
	public String changeName(String uid, String uname)
	{
		return arbitrationServiceImpl.changeName(uid, uname);
	}

	/**
	 * 修改地址
	 *
	 * @param uid
	 * @param uaddress
	 * @return
	 */
	@ApiOperation(value = "修改地址", notes = "修改地址")
	@OperationLogDetail(detail = "修改地址", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	@PostMapping("/api-front/arbitration/changeAddress")
	@ResponseBody
	public String changeAddress(String uid, String uaddress)
	{
		return arbitrationServiceImpl.changeAddress(uid, uaddress);
	}

	/**
	 * 修改简介
	 *
	 * @param uid
	 * @param ustory
	 * @return
	 */
	@ApiOperation(value = "修改简介", notes = "修改简介")
	@OperationLogDetail(detail = "修改简介", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	@PostMapping("/api-front/arbitration/changeUstory")
	@ResponseBody
	public String changeUstory(String uid, String ustory)
	{
		return arbitrationServiceImpl.changeStory(uid, ustory);
	}

	/**
	 * 订单申请仲裁
	 *
	 * @param orderid
	 * @param cause
	 * @return
	 */
	@ApiOperation(value = "订单申请仲裁", notes = "订单申请仲裁")
	@OperationLogDetail(detail = "订单申请仲裁", level = 3, operationUnit = OperationUnit.USER, operationType = OperationType.INSERT)
	@PostMapping("/api-front/arbitration/applyArbitration")
	@ResponseBody
	public String applyArbitration(String orderid, String cause)
	{
		return arbitrationServiceImpl.applyArbitration(orderid, cause);
	}

	/**
	 * 查看订单仲裁处理表格
	 *
	 * @param starttime
	 * @param endtime
	 * @param arbitrationType
	 * @param request
	 * @param req
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('front:process:query')")
	@ApiOperation(value = "查看订单仲裁处理表格", notes = "查看订单仲裁处理表格")
	@OperationLogDetail(detail = "查看订单仲裁处理表格", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/api-front/arbitration/arbitrationTable")
	@ResponseBody
	public Results<Arbitration> arbitrationTable(String starttime, String endtime, String arbitrationType, HttpServletRequest request, PageTableRequest req)
	{
		req.countOffset();
		return arbitrationServiceImpl.arbitrationTable(req.getOffset(), req.getLimit(), starttime, endtime, arbitrationType, request);
	}

	/**
	 * 查看订单仲裁投票
	 *
	 * @param starttime
	 * @param endtime
	 * @param request
	 * @param req
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('front:vote:query')")
	@ApiOperation(value = "查看订单仲裁投票", notes = "查看订单仲裁投票")
	@OperationLogDetail(detail = "查看订单仲裁投票", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/api-front/arbitration/voteTable")
	@ResponseBody
	public Results<Arbitration> voteTable(String starttime, String endtime, HttpServletRequest request, PageTableRequest req)
	{
		req.countOffset();
		return arbitrationServiceImpl.voteTable(req.getOffset(), req.getLimit(), starttime, endtime, request);
	}

	/**
	 * 查看订单详情
	 *
	 * @param oid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('front:process:details')")
	@ApiOperation(value = "查看订单详情", notes = "查看订单详情")
	@OperationLogDetail(detail = "查看订单详情", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/api-front/arbitration/details")
	@ResponseBody
	public Orders details(String oid)
	{
		return exposureServiceImpl.exposureDetails(oid);
	}

	/**
	 * 订单进行仲裁投票操作
	 *
	 * @param aid
	 * @param operate
	 * @param request
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('front:vote:vote')")
	@ApiOperation(value = "订单进行仲裁投票操作", notes = "订单进行仲裁投票操作")
	@OperationLogDetail(detail = "订单进行仲裁投票操作", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@RequestMapping("/api-front/arbitration/vote")
	@ResponseBody
	public String vote(String aid, String operate, HttpServletRequest request)
	{
		return arbitrationServiceImpl.vote(aid, operate, request);
	}


	/**
	 * 新增仲裁
	 *
	 * @param userDto
	 * @return
	 */
	@PostMapping("/api-back/arbitration/addArbitration")
	@ResponseBody
	@ApiOperation(value = "新增仲裁员信息", notes = "新增仲裁员信息", httpMethod = "POST", response = Results.class)
	@OperationLogDetail(detail = "新增仲裁员信息", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	public Results<SysUser> saveUser(UserDto userDto)
	{
		SysUser user = null;
		user = userServiceImpl.getUserByPhone(userDto.getUphone());
		if (user != null && !(user.getUid().equals(userDto.getUid())))
		{
			return Results.failure(ResponseCode.PHONE_REPEAT.getCode(), ResponseCode.PHONE_REPEAT.getMessage());
		}
		user = userServiceImpl.getUser(userDto.getUaccount());
		if (user != null && !(user.getUid().equals(userDto.getUid())))
		{
			return Results.failure(ResponseCode.USERNAME_REPEAT.getCode(), ResponseCode.USERNAME_REPEAT.getMessage());
		}
		//spring security的密码加密
		userDto.setUpassword(new BCryptPasswordEncoder().encode(userDto.getUpassword()));
		userDto.setRid(5);
		return userServiceImpl.save(userDto, 5, 1);

	}

	/**
	 * 跳转页面
	 *
	 * @return
	 */
	@GetMapping("api-back/arbitration/openAdd")
	public String openAdd()
	{
		return "backPage/arbitration-reg";
	}

	/**
	 * 查看仲裁员列表
	 *
	 * @param req
	 * @param aname
	 * @param aaccount
	 * @param userStatus
	 * @return
	 */
	@ApiOperation(value = "查看仲裁员列表", notes = "查看仲裁员列表")
	@OperationLogDetail(detail = "查看仲裁员列表", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/api-back/arbitration/userTable")
	@ResponseBody
	public Results<SysUser> userTable(PageTableRequest req, String aname, String aaccount, String userStatus)
	{
		req.countOffset();
		return arbitrationServiceImpl.userTable(req.getOffset(), req.getLimit(), aname, aaccount, userStatus);
	}

	/**
	 * 仲裁员禁用启用
	 *
	 * @param uid
	 * @param operate
	 * @return
	 */
	@ApiOperation(value = "后台仲裁员禁用启用", notes = "后台仲裁员禁用启用")
	@OperationLogDetail(detail = "后台仲裁员禁用启用", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/arbitration/arbitratorOperate")
	@ResponseBody
	public String arbitratorOperate(String uid, String operate)
	{
		return arbitrationServiceImpl.arbitratorOperate(uid, operate);
	}

	/**
	 * 后台对仲裁员重置密码
	 *
	 * @param uid
	 * @param operate
	 * @return
	 */
	@ApiOperation(value = "后台对仲裁员重置密码", notes = "后台对仲裁员重置密码")
	@OperationLogDetail(detail = "后台对仲裁员重置密码", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/api-back/arbitration/repass")
	@ResponseBody
	public String repass(String uid)
	{
		String repass = new BCryptPasswordEncoder().encode("123456");
		return arbitrationServiceImpl.repass(uid, repass);
	}

}
